package cn.edu.sdjzu.xg.eagle.controller;

import cn.edu.sdjzu.xg.eagle.Service.TeacherService;
import cn.edu.sdjzu.xg.eagle.entity.Pagination;
import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.util.Condition;
import cn.edu.sdjzu.xg.eagle.util.ControllerHelper;
import cn.edu.sdjzu.xg.eagle.util.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet("/teacherRead")
public class TeacherReadController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        JSONObject message_jsonObj = new JSONObject();
        response.setContentType("text/html;charset=utf-8");
        String req_jsonStr= JSONUtil.getJSON(request);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if(req_jsonObj == null){
            message_jsonObj.put("message","未收到请求消息");
            response.getWriter().println(message_jsonObj);
            return;
        }
        try {
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            Collection<Teacher> teacherList = TeacherService.getInstance().findAll(conditionList);
            String teachers_json = JSON.toJSONString(teacherList, JSONWriter.Feature.ReferenceDetection);
            response.getWriter().println(teachers_json);
        }catch (BysjException e) {
            message_jsonObj.put("message", e.getMessage());
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        JSONObject message_jsonObj = new JSONObject();
        response.setContentType("text/html;charset=utf-8");
        String req_jsonStr= JSONUtil.getJSON(request);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if(req_jsonObj == null){
            message_jsonObj.put("message","未收到请求消息");
            response.getWriter().println(message_jsonObj);
            return;
        }
        try{
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            Collection<Teacher> teachers =TeacherService.getInstance().findAll(conditionList,pagination);
            message_jsonObj.put("totalNum",pagination.getTotalNum());
            message_jsonObj.put("records",teachers);
            response.getWriter().println(message_jsonObj);
        }catch (BysjException e){
            message_jsonObj.put("message", e.getMessage());
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }
    }

}
